package com.example.demo.controller

import jakarta.websocket.OnClose
import jakarta.websocket.OnMessage
import jakarta.websocket.OnOpen
import jakarta.websocket.Session
import jakarta.websocket.server.ServerEndpoint
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import java.io.IOException


@ServerEndpoint("/myWs")
@Component
class WsServerEndpoint {

    //    @Autowired
//    private lateinit var log: Logger
    private var log = LoggerFactory.getLogger("logger")


    /**
     * 连接成功
     *
     * @param session
     */
    @OnOpen
    fun onOpen(session: Session?) {
        MyController.tempSession = session
        log.info("连接成功")
    }

    /**
     * 连接关闭
     *
     * @param session
     */
    @OnClose
    fun onClose(session: Session?) {
        log.info("连接关闭")
    }


    /**
     * 接收到消息
     *
     * @param text
     */
    @OnMessage
    @Throws(IOException::class)
    fun onMsg(text: String): String {
        log.info("text:${text}")
        return "servet 接受：$text"
    }
}